$(function(){
	var accounts=sessionStorage.getItem('correlationaccount'),
	base=sessionStorage.getItem("base"),
	selectedAccounts=[],
	mainAccountIndex=-1, 
	selected=[],
	paramJSON=utils.createQueryStringJSONObject(location.search.substr(1),true),
//	accountHtml='<div class="account_list_ul"><div class="account_list_li"><a appkey="appKey" custaccount="cust_account" ind="index"><div class="account_content"><h2>name</h2><p>注册来源：<span>appName</span>账户余额：<span>account_balance元 </span></p></div></a><i><img src="images/tipss.png" width="19" height="19"></i></div>$rscontent</div>',
	accountHtml='<div class="account_list_ul"><div class="account_list_li"><a appkey="appKey" custaccount="cust_account" ind="index" class="rowclass2"><div class="account_content"><h2>name</h2><p>账户余额：<span>account_balance </span></p></div></a></div></div>',
	accountResourceHtml='<div class="account_table"><table><tr><th scope="col">应用名称</th><th scope="col">产品名称</th><th scope="col">资源可用量</th><th scope="col">类型</th></tr>$content</table></div>';	   
	if(accounts){
		accounts=JSON.parse(accounts);
		selected=JSON.parse(paramJSON.data);
		var htmls=[],custaccounts=selected.join(',');
		for (var i = 0; i < accounts.length; i++) {
			if(custaccounts.indexOf(accounts[i].cust_account)>-1){//只展示已经选择的账户
				selectedAccounts.push({
					app_key:accounts[i].appKey,
					cust_account:accounts[i].cust_account,
					is_lord_account:'0'
				});
				var ah=accountHtml.replace(/name|appName|account_balance|appKey|cust_account|index|class2/g,function(value){
					if(value==='index')
						return (selectedAccounts.length-1);
					else if(value==='class2'){
						return (selected.length===1)?' checked':'';
					}else
					    return accounts[i][value]||value;
				}),rsh='';
//				var resources=accounts[i].resources,html=[];
//				if(resources&&resources.length>0){
//					for (var m = 0; m < resources.length; m++) {
//						html.push('<tr><td>',accounts[i].appName,'</td>');
//						html.push('<td>',resources[m].product_name,'</td>');
//						html.push('<td>',resources[m].balance,'</td>');
//						html.push('<td>',resources[m].resource_type,'</td></tr>');
//					}
//					rsh=accountResourceHtml.replace('$content',html.join(''));
//				}
//				htmls.push(ah.replace('$rscontent',rsh));
				htmls.push(ah);
			}
		}
		$('#content').append(htmls.join(''));
	}
	
	  var showHeader=JSON.parse(sessionStorage.getItem('parameter')).showHeader||'1';//不传默认展示标题栏  
	  if(showHeader==='1'){  
		  $('#header').css('display','block'); 
		  $('#content').css('margin-top','43px');
	  }
	  
	  $(document.body).on('click','.account_list_ul .account_list_li i',function(){
		  var oTable=$(this).parent('.account_list_li').siblings('.account_table');	
		  var oI=$(this);
		  if(oTable.css('display')=='none'){
			  oI.css('opacity','0.6');
			  $(this).parent('.account_list_li').siblings('.account_table').slideToggle(500); 
		  }else if(oTable.css('display')=='block'){
			  oI.css('opacity','1');
			  $(this).parent('.account_list_li').siblings('.account_table').slideToggle(500); 
		  }		  
	  });
	  
	  $(document.body).on('click','.account_list_li a',function(){
		  var ac=$(this);
		  if(ac.hasClass('checked')){
			  ac.removeClass('checked');
			  selectedAccounts[mainAccountIndex].is_lord_account='0';
			  mainAccountIndex=-1;
		  }else{
			  $('.account_list_ul a').each(function(){
				  var $this=$(this);
				  if($this.attr('custaccount')!==ac.attr('custaccount')){
					  $this.removeClass('checked');
				  }
			  });
			  ac.addClass('checked');
			  mainAccountIndex=ac.attr('ind');
			  selectedAccounts[mainAccountIndex].is_lord_account='1';
		  }
	  });
	  	  
	  $('#combineAccounts').on('click',function(){
		  if(mainAccountIndex===-1){
		    	new UserPrompt('请选择需要合并的主账户');
		        return;
		  }
		  var acc=selectedAccounts.splice(mainAccountIndex,1)[0];
		  var basePara=JSON.parse(sessionStorage.getItem('parameter')),
		  cust_account_old=basePara.cust_account;
		  $.extend(basePara,{
			  cust_account:acc.cust_account,//主账户
			  cust_account_old:cust_account_old,//加密用账户
			  accounts:JSON.stringify(selectedAccounts)
		  });
		  console.log('data==='+JSON.stringify(basePara));
	      $.ajax({
		      type : "get",
	          dataType : "jsonp",
		      url : base+"orgacc/orgAccMerge.do",
		      data :basePara,
		      success:function(data){
				  console.log('datareturn==='+JSON.stringify(data));
		    	  if(data.result==='0'){
		              location.assign('success.html?message=账号合并操作成功&title=账号合并'); 
		    	  }				  
		      },
		      error:function(data){
		    	  if(data&&data.describe)
			        new UserPrompt(data.describe);
		      }
		  });
	  });
	  
	  $('#back').on('click',function(){
		  history.back();
	  }); 
	    
//	  $('#content').css({
//		  'height':utils.getHeight([$('#header'),$('#combineAccounts')]),
//		  'overflow-y':'auto'
//	  });
	  
});